package com.cal2sync;

import java.util.Calendar;

import com.cal2sync.R;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class last_screen extends Activity {
	/** Called when the activity is first created. */
	LinearLayout l;
	private PendingIntent pendingIntent;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.last_screen);

		l=(LinearLayout) findViewById(R.id.main);
		final Animation slide = AnimationUtils.loadAnimation(this, R.anim.righttoleft);
		final Animation inslide = AnimationUtils.loadAnimation(this, R.anim.lefttoright);

		l.startAnimation(inslide);
		inslide.setFillAfter(true);
		inslide.setAnimationListener(new AnimationListener(){
			public void onAnimationEnd(Animation animation){

				final Button button1 = (Button) findViewById(R.id.create_rule_btn);
				button1.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						// Perform action on click
						l.startAnimation(slide);
						slide.setFillAfter(true);
						slide.setAnimationListener(new AnimationListener(){
							public void onAnimationEnd(Animation animation){
								Intent i = new Intent(getBaseContext(),create_rule.class);
								startActivity(i); 
							}

							@Override
							public void onAnimationRepeat(Animation arg0) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onAnimationStart(Animation animation) {
								// TODO Auto-generated method stub

							}
						});
					}
				});

				final Button button2 = (Button) findViewById(R.id.exit_btn);
				button2.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						// Perform action on click
						Intent myIntent = new Intent(last_screen.this, GetCalendarEvents.class);
						pendingIntent = PendingIntent.getService(last_screen.this, 0, myIntent, 0);
						AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
						Calendar calendar = Calendar.getInstance();
    			        calendar.setTimeInMillis(System.currentTimeMillis());
					//	calendar.add(Calendar.MINUTE, 5);
    			        calendar.add(Calendar.SECOND, 5);
					//	alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
						alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*1000, pendingIntent);
					
						//Toast.makeText(last_screen.this, "Start Alarm", Toast.LENGTH_LONG).show();
						//GetCalendarEvents.getCalendarEvents(last_screen.this);
						Intent intent = new Intent(Intent.ACTION_MAIN);
						intent.addCategory(Intent.CATEGORY_HOME);
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intent); 
					}
				});

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}
		});
	}
}